﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Portal.Presentation.Tests.Units;
using Portal.Presentation.Services;
using Portal.Logic.Providers;
using Portal.Model;
using Rhino.Mocks;
using FluentAssertions;

namespace Portal.Presentation.Providers.Tests.Units
{
    [TestClass]
    public class SecurityProviderTests : BaseTests
    {

        private ISessionService _session;
        private IUserProvider _userProvider;

        protected override void InitializeTest()
        {
            _session = Mocks.DynamicMock<ISessionService>();
            _userProvider = Mocks.DynamicMock<IUserProvider>();
        }

        #region GetUser

        [TestMethod()]
        public void GetUser_UserExistInSession_ReturnUser()
        {
            var logon = "logon";
            var user = Mocks.DynamicMock<IUser>();
            SetupResult.For(user.Login).Return(logon);
            Expect.Call(_session.GetCurrentUser()).Return(user);
            Mocks.ReplayAll();

            var provider = GetProvider();
            var result = provider.GetUser(logon);

            result.Should().NotBeNull();
            result.Login.Should().Be(logon);
        }

        [TestMethod()]
        public void Header_UserIsProvided_ReturnUser()
        {
            var logon = "logon";
            var user = Mocks.DynamicMock<IUser>();
            SetupResult.For(user.Login).Return(logon);
            Expect.Call(_session.GetCurrentUser()).Return(null);
            Expect.Call(_userProvider.GetUser(logon)).Return(user);
            Mocks.ReplayAll();

            var provider = GetProvider();
            var result = provider.GetUser(logon);

            result.Should().NotBeNull();
            result.Login.Should().Be(logon);
        }

        #endregion

        private SecurityProvider GetProvider()
        {
            return new SecurityProvider(_userProvider, _session);
        }
    }
}
